﻿using PSAMMatrixCloud.Model;
using System;
using System.Linq;
using System.Text;

namespace PSAMMatrixCloud.TransferSecret.PSAMMatrix 
{
    public class Message
    {
        public static byte[] ProtocolHead = { 0x55, 0xAA };

        public Message(int commandType, byte messageID,bool bInit=false) 
        {
            CommandType = 0xC0;
            MessageID = messageID;
            switch ((DeviceCommandTypeAsk)commandType)
            {
                case DeviceCommandTypeAsk.VehicleInfo:
                    if (bInit)
                    {
                        Body = new byte[32];
                    }
                    else
                    {
                        Body = new byte[88];
                    }
                    break;

                case DeviceCommandTypeAsk.MAC1:
                    Body = new byte[52];
                    break;

                case DeviceCommandTypeAsk.MAC2:
                    Body = new byte[20];
                    break;
                default:
                    Body = new byte[0];
                    break;
            }
        }

        public Message(byte[] data)  
        {
            byte[] headBytes = new byte[2];
            byte[] lengthBytes = new byte[2];
            byte[] messageIDBytes = new byte[1];
            byte[] typeBytes = new byte[1];
            Body = new byte[data.Length - 7];

            Array.ConstrainedCopy(data, 0, headBytes, 0, 2);
            Array.ConstrainedCopy(data, 2, messageIDBytes, 0, 1);
            Array.ConstrainedCopy(data, 3, lengthBytes, 0, 2);
            Array.ConstrainedCopy(data, 5, typeBytes, 0, 1);
            Array.ConstrainedCopy(data, 6, Body, 0, data.Length - 7);
            CheckValue = data[data.Length - 1];
            Length = data.Length - 7;
            MessageID = messageIDBytes[0];
            CommandType = typeBytes[0];
            HeadInfo = headBytes;
        }

        /// <summary>
        /// 头信息
        /// </summary>
        public byte[] HeadInfo { get; set; }

        /// <summary>
        /// 数据主体
        /// </summary>
        public byte[] Body { get; set; }

        /// <summary>
        /// 指令类型
        /// </summary>
        public byte CommandType { get; set; }

        public long Length { get; set; }

        /// <summary>
        /// 消息ID
        /// </summary>
        public byte MessageID { get; set; }



        /// <summary>
        /// 校验值
        /// </summary>
        public byte CheckValue { get; set; }

        public void Clear()
        {
            Body = new byte[0];
        }

        public byte[] ToBytes()
        {
            byte[] buffer = new byte[Body.Length + 7];

            byte[] lengthBytes = BitConverter.GetBytes((short)Body.Length);
            byte messageIDBytes = MessageID;

            if (BitConverter.IsLittleEndian) // 若为 小端模式
            {
                Array.Reverse(lengthBytes); // 转换为 大端模式               
            }
            Array.ConstrainedCopy(ProtocolHead, 0, buffer, 0, 2);
            //buffer[2] = MessageID;
            buffer[2] = 0x00;
            Array.ConstrainedCopy(lengthBytes, 0, buffer, 3, 2);

            buffer[5] = CommandType;

            if (Body != null && Body.Length != 0)
            {
                Array.ConstrainedCopy(Body.ToArray(), 0, buffer, 6, Body.Length);
            }
            //计算校验
            buffer[buffer.Length - 1] = (byte)PSAMMatrixCloud.Common.Check.CheckXor(buffer, 2, buffer.Length - 3);
            return buffer;
        }
    }
}